{{define "admin/resource.html"}}
<!DOCTYPE html>
<html>

<head>
  {{template "layout/head.html" .}}
  <style type="text/css">
  
      .btn-white{
          background-color:#fff!important;
      }
      .bootstrap-select .dropdown-menu{width:90%;}
  </style>
</head>

<body>
    <div class="wrapper">
        <div class="content-wrapper">
            <section class="content">
                <div class="container-fluid">
                    <div class="row" style="padding-bottom: 5px;">
                        <div class="col-md-6">
                            <div class="form-horizontal searchPanel  form-inline"  >
                                <label class=" control-label" style="padding-left:0px; ">应用:</label>
                                <div class="btn-group" style="padding-left: 0px;">
                                    <select data-name="appid" class="form-control  feild-edit appidSelect" style="width: 150px;"
                                        onchange="resource_operat.AppIdchange($(this).val())">
                                    </select>
                                </div>
                            </div>            
                        </div>
                    </div>
                    <div class="row">
                        <!-- left column -->
                        <div class="col-md-6">
                            <div>
                                <span style="margin-right:20px">颜色含义：</span>
                                <span style="color: red!important;margin-right:20px">分类</span>
                                <span style="color: blue!important;margin-right:20px">页面</span>
                                <span style="color:green!important;margin-right:20px">页面内功能</span>
                            </div>
                            <div id="RightTreePannel" class=" ztree"></div>
                        </div>
                        <!-- right column -->
                        <div class="col-md-6">
                            <div class="box box-info" id="RightEditPannel" style="min-height: 400px;">
                                <div class="box-header with-border">
                                    <h3 class="box-title"><span class="text-green" id="cur_node_path"></span></h3>
                                </div>
                                <div class="box-body">
                                    <form class="form-horizontal">
                                        <div class="form-group row" style="display: none;">
                                            <label class="col-sm-2 control-label"  for="rightid">权限id：</label>
                                            <input  id="rightid" value="0" class="form-control  col-sm-10" type="text"  disabled="disabled"/>
                                        </div>
                                        <div class="form-inline " style="display: none;">
                                            <label class="col-sm-2 control-label">父节点ID：</label>
                                            <input id="rightparentid" value="0" class="form-control  col-sm-10" type="number"   disabled="disabled"/>
                                        </div>
                                        <!--<div class="form-inline"><label  class="col-sm-2 control-label">权限级别：</label><input id="rightLvL" value="0" class="form-control  col-sm-10" type="text" disabled="disabled" /></div>-->
                                        <div class="form-group row">
                                            <label class="col-sm-2 control-label" for="rightname">权限名称：</label>
                                            <div class="col-sm-10">
                                                <input type="text" id="rightname" class="form-control  control-block"/>
                                            </div>
                                        </div>
                                        <div class="form-group row"><label class="col-sm-2 control-label">权限：</label>
                                            <div class="col-sm-10">
                                                <select id="rightUrl" class="form-control selectpicker  control-block" ></select>
                                                <label> 控制器中定义的权限标识，如：system:authorize:resource:get</label>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-sm-2 control-label">描述：</label>
                                            <div class="col-sm-10"><textarea id="nodeDescribe" class="form-control  control-block wysihtml5" rows="5"></textarea></div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-sm-2 control-label" for="rightname">排序：</label>
                                            <div class="col-sm-10">
                                                <input type="number" id="sortid" class="form-control  control-block "/>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-sm-2 control-label">类型：</label>
                                            <div class="col-sm-10">
                                                <select id="nodeMenu" class="form-control  control-block">
                                                <option value="1" selected="selected">页面内功能</option>
                                                <option value="2">页面</option>
                                                <option value="3">分类</option>
                                            </select>
                                            </div>
                                        </div>
                                        <!--<div class="form-group row" ><label  class="col-sm-2 control-label">所属模块：</label><select id="nodeCategory"></select></div>-->
            
            
                                        <div class="form-group row" id="FuncNode_errorNote" style="display: none;">
                                            <span class="text-red">Text red to emphasize danger</span>
                                        </div>
            
                                        <div style="text-align: right;">
                                            <input type="button" id="RightEditPannel_addbtn" value="添加" style="display:none"
                                                class="btn bg-olive btn-flat "/>
                                            <input type="button" id="RightEditPannel_editbtn" value="修改" style="display:none"
                                                class="btn bg-olive btn-flat "/>
                                        </div>
                                    </form>
                                </div>
            
                            </div>
                        </div>
            
                    </div>
                </div>
            </section>

        </div>
    </div>



  {{template "layout/foot.html" .}}
  {{template "layout/iframePageFoot.html" .}}

  <link href="{{.staticResourcesPathJs}}/static/plugins/ztree_v3/css/metroStyle/metroStyle.css" rel="stylesheet"/>
  <script src="{{.staticResourcesPathJs}}/static/plugins/ztree_v3/js/jquery.ztree.all.js"></script>
    
  <link rel="stylesheet" type="text/css" href="{{.staticResourcesPathJs}}/static/plugins/bootstrap-select/css/bootstrap-select.min.css">
  <script type="text/javascript" src="{{.staticResourcesPathJs}}/static/plugins/bootstrap-select/js/bootstrap-select.min.js"></script>

    <script type="text/javascript">
        var resource_operat={
            zTreeSetting:{
                view: {
                    selectedMulti: false,
                    showLine: true,
                    showText: true,
                    showIcon: false,
                    addHoverDom: addHoverDom,
                    removeHoverDom: removeHoverDom,
                    fontCss: function (treeId, treeNode) {
                        if (treeNode.pType == 1) {
                            return {color: "green"};
                        } else if (treeNode.pType == 2) {
                            return {color: "blue"};
                        } else if (treeNode.pType == 3) {

                            return {color: "red"};
                        }
                    }
                }
            },
            zTreeObj:null,
            appid:0,
            apps:{
                datas:[],
                getName : function (value) {
                    var result = '';
                    $(this.datas).each(function (i, vn) {
                        if (vn.pId == value) {
                            result = vn.pName;
                            return false;
                        }
                    });
                    return result;
                }
            },
            Right_permissions:[]
        }
        resource_operat.pageInit=function(){   
            resource_operat.InitAppsData(function(){
                $(resource_operat.apps.datas).each(function (i,appID) {
                    if(i==0){
                        $(".appidSelect").append('<option value="'+appID.pId+'" selected>'+appID.pName+'</option>');
                        resource_operat.AppIdchange(appID.pId);
                    }else{
                        $(".appidSelect").append('<option value="'+appID.pId+'">'+appID.pName+'</option>');
                    }
                })         
            })
            
            $("#rightUrl").selectpicker({
                noneSelectedText: '未选择',
                liveSearch: true,
                style: 'btn-default btn-white',
                size:20
            });
            
        }
        resource_operat.AppIdchange=function (appid) {
            resource_operat.appid=appid;
            resource_operat.zTreeObj=null;
            resource_operat.Right_URL=[];
            resource_operat.loadTree();
        }
       
        resource_operat.InitAppsData = function (callback) {
            $.ajax({
                url:  page_content_path+'/api/system/authorize/app/getByPage',
                data:{pageIndex:1,pageSize:10000000,state:1},
                type: 'get',
                cache: true,
                success: function (result) {
                    if (!joyconn_layout.ValidataResult(result)) {
                        return;
                    }
                    if (result.data){
                        resource_operat.apps.datas=result.data;
                        if(resource_operat.apps.length>0){
                            resource_operat.appid = resource_operat.apps[0].pId
                        }
                    }
                    if(callback){
                        callback();
                    }
                }

            });
        }
        resource_operat.loadTree=function () {
            $.ajax({
                url: page_content_path+ '/api/system/authorize/resource/getTree?appid='+resource_operat.appid,
                type: 'get',
                cache: false,
                success: function (txt) {
                    if (!joyconn_layout.ValidataResult(txt)) {
                        return;
                    }
                    if (txt != '') {
                        var node = txt.data;
                        if (node != null) {
                            resource_operat.FillnodeName(node,);
                            resource_operat.zTreeObj = $.fn.zTree.init($("#RightTreePannel"), resource_operat.zTreeSetting, node);

                        }
                    }

                }
            });
        }

        //将后台输出的实体类添加name children title属性
        resource_operat.FillnodeName = function ( node) {
            node.name = node.pName;
            node.tId = node.pId;

            //node.children = node.Children;
            node.title = node.pDesc;
            if (node.pId == 0) {
                node.name = resource_operat.apps.getName(resource_operat.appid) +" - "+ node.pName;
                node.open = true;
            } else {
                node.open = false;
            }
            if(node.pPermission){
                resource_operat.Right_permissions.push(node.pPermission);
            }
            if (!node.children) {
                node.children = [];
            }
            if (node.children.length) {
                for (var i = 0; i < node.children.length; i++) {

                    resource_operat.FillnodeName(node.children[i]);
                }
            }

        }

        //节点点击事件
        function nodeClick  (event, treeId, treeNode) {

        }
        //为树的节点添加鼠标悬浮效果
        function  addHoverDom  (treeId, treeNode) {
            var sObj = $("#" + treeNode.tId + "_span");
            if (treeNode.editNameFlag || $("#diySpanStr_" + treeNode.tId).length > 0) return;
            var diySpanStr = "<span id='diySpanStr_" + treeNode.tId + "'>";
            diySpanStr += "<span class='button add' id='addBtn_" + treeNode.tId + "' title='添加子权限' onfocus='this.blur();'></span>";
            if (treeNode.Id != 0)
                diySpanStr += "<span class='button edit' id='editBtn_" + treeNode.tId + "' title='修改权限' onfocus='this.blur();'></span>";
            if (treeNode.children && treeNode.children.length == 0) {
                diySpanStr += "<span class='button remove' id='removeBtn_" + treeNode.tId + "' title='删除权限' onfocus='this.blur();'></span>";
            }
            diySpanStr += "</span>";

            sObj.after(diySpanStr);

            //添加按钮
            var addbtn = $("#addBtn_" + treeNode.tId);
            if (addbtn) addbtn.bind("click", function () {
                resource_operat.initPaths();
                var path = '在"<b>' + resource_operat.getNodePath(treeNode) + '</b>"下添加新节点';
                $("#cur_node_path").html(path);
                $("#FuncNode_errorNote").css("display", "none");
                $("#rightid").val("0");
                $("#rightparentid").val(treeNode.pId);
                // $("#rightLvL").val(treeNode.Level + 1);
                $("#rightname").val("");
                $("#rightUrl").selectpicker('val','')
                $("#nodeDescribe").val("");
                $("#nodeMenu").val(1);
                $("#sortid").val(1);
                $('#RightEditPannel_addbtn').unbind("click");
                $('#RightEditPannel_addbtn').css("display", "");
                $('#RightEditPannel_addbtn').attr("disabled", false);
                $('#RightEditPannel_editbtn').css("display", "none");
                var level = parseInt(treeNode.pLevel) + 1;
                $('#RightEditPannel_addbtn').unbind("click").bind("click", function () {
                    if ($("#rightname").val() == '') {
                        $("#FuncNode_errorNote").css("display", "");
                        $("#FuncNode_errorNote").find("span").html("请输入权限名");
                        return;
                    }
                    var permission = $("#rightUrl").selectpicker('val');
                    if (!permission) {
                        $("#FuncNode_errorNote").css("display", "");
                        $("#FuncNode_errorNote").find("span").html("请输入权限的资源ID");
                        return;
                    }
                    $('#RightEditPannel_addbtn').attr("disabled", "disabled");
                    var tnode = {
                        pName: encodeURI($("#rightname").val()),
                        pPermission: permission,
                        pPid: $("#rightparentid").val()*1,
                        pDesc: encodeURI($("#nodeDescribe").val()),
                        pType: $("#nodeMenu").val()*1,
                        pLevel: level,
                        pAppid:resource_operat.appid,
                        pSort:$("#sortid").val()*1
                    };

                    $("#FuncNode_errorNote").css("display", "");
                    JoyDialog.showLoading();
                    $.ajax({
                        url:  page_content_path+'/api/system/authorize/resource/add',
                        data:JSON.stringify(tnode) ,
                        contentType:'application/json',
                        type: 'post',
                        cache: false,
                        success: function (txt) {
                            JoyDialog.hideLoading();
                            if (!joyconn_layout.ValidataResult(txt)) {
                                return;
                            }

                            $('#RightEditPannel_addbtn').attr("disabled", false);
                            if (txt.data > 0) {

                                var insertNodes = [];

                                tnode.pId = txt.data;
                                tnode.pName = $("#rightname").val();
                                tnode.pDesc = $("#nodeDescribe").val();
                                tnode.pPid = treeNode.pId;
                                tnode.children = [];
                                tnode.name = tnode.pName;
                                tnode.tId = tnode.pId;
                                tnode.title = tnode.pDesc
                                tnode.pAppid=resource_operat.appid;
                                tnode.pPermission = permission;
                                insertNodes.push(tnode);
                                resource_operat.FillnodeName(insertNodes);
                                resource_operat.zTreeObj.addNodes(treeNode, tnode, true);
                                resource_operat.zTreeObj.expandNode(treeNode,true);
                                
                                resource_operat.Right_permissions.push(permission);
                                $("#FuncNode_errorNote").css("display", "none");
                                resource_operat.initPaths();
                                JoyDialog({
                                    type:"toast_success",
                                    infoText: '添加成功！',
                                });

                            }else if (txt.data == 0) {
                                JoyDialog({
                                    type:"toast_warning",
                                    infoText: data.msg,
                                });
                            } else if (txt.data == -2) {
                                $("#FuncNode_errorNote").css("display", "");
                                $("#FuncNode_errorNote").find("span").html("URl已存在，请更改");
                            }
                            else {
                                $("#FuncNode_errorNote").css("display", "");
                                $("#FuncNode_errorNote").find("span").html("添加失败");
                            }
                        }
                    });
                });
                return false;
            });
            //编辑按钮
            var editbtn = $("#editBtn_" + treeNode.tId);
            if (editbtn) editbtn.bind("click", function () {
                var path = '修改 "<b>' + resource_operat.getNodePath(treeNode) + '</b>"';
                $("#cur_node_path").html(path);
                $("#FuncNode_errorNote").css("display", "none");
                $("#rightid").val(treeNode.pId);
                $("#rightparentid").val(treeNode.pPid);
                $("#rightname").val(treeNode.pName);
                resource_operat.initPaths(treeNode.pPermission);
                $("#nodeDescribe").val(treeNode.pDesc);
                $('#nodeMenu').val(treeNode.pType);
                $("#sortid").val(treeNode.pSort);
                $('#RightEditPannel_editbtn').unbind("click");
                $('#RightEditPannel_editbtn').css("display", "");
                $('#RightEditPannel_editbtn').attr("disabled", false);
                $('#RightEditPannel_addbtn').css("display", "none");
                $('#RightEditPannel_editbtn').unbind("click").bind("click", function () {
                    if ($("#rightname").val() == '') {
                        $("#FuncNode_errorNote").css("display", "");
                        $("#FuncNode_errorNote").find("span").html("请输入权限名");
                        return;
                    }
                    var permission = $("#rightUrl").selectpicker('val');
                    if (!permission) {
                        $("#FuncNode_errorNote").css("display", "");
                        $("#FuncNode_errorNote").find("span").html("请输入权限的资源ID");
                        return;
                    }
                    $('#RightEditPannel_editbtn').attr("disabled", "disabled");

                    $("#FuncNode_errorNote").css("display", "");
                    JoyDialog.showLoading();
                    $.ajax({
                        url: page_content_path+ '/api/system/authorize/resource/update',
                        data:JSON.stringify({
                            pId: treeNode.pId,
                            pName: encodeURI($("#rightname").val()),
                            pPid: treeNode.pPid,
                            pLevel: treeNode.pLevel,
                            pAppid:treeNode.pAppid,
                            pType: $('#nodeMenu').val()*1,
                            pDesc: encodeURI($("#nodeDescribe").val()),
                            pPermission: permission,
                            pSort:$("#sortid").val()*1
                        }) ,
                        contentType:'application/json',
                        type: 'post',
                        cache: false,

                        success: function (txt) {
                            JoyDialog.hideLoading();
                            if (!joyconn_layout.ValidataResult(txt)) {
                                return;
                            }

                            $('#RightEditPannel_editbtn').attr("disabled", false);


                            if (txt.data > 0) {
                                resource_operat.Right_permissions.splice($.inArray(treeNode.pPermission, resource_operat.Right_permissions), 1);
                                treeNode.name = $("#rightname").val();
                                treeNode.pName = $("#rightname").val();
                                treeNode.pType = $('#nodeMenu').val()*1;
                                treeNode.pDesc = $("#nodeDescribe").val();
                                treeNode.pPermission = permission;
                                treeNode.title = treeNode.pDesc;
                                treeNode.pSort = $("#sortid").val()*1
                                resource_operat.Right_permissions.push(treeNode.pPermission);
                                $("#FuncNode_errorNote").css("display", "none");
                                JoyDialog({
                                    type:"toast_success",
                                    infoText: '修改成功！',
                                });
                                resource_operat.zTreeObj.refresh()
                            }else if (txt.data == 0) {
                                JoyDialog({
                                    type:"toast_warning",
                                    infoText: data.msg,
                                });
                            }  else if (txt.data == -2) {

                                $("#FuncNode_errorNote").css("display", "");
                                $("#FuncNode_errorNote").find("span").html("URl已存在，请更改");
                            }
                            else {
                                $("#FuncNode_errorNote").css("display", "");
                                $("#FuncNode_errorNote").find("span").html("修改失败");
                            }
                        }
                    });
                });
                return false;
            });
            //删除按钮
            var removebtn = $("#removeBtn_" + treeNode.tId);
            if (removebtn) removebtn.bind("click", function () {
                var path = resource_operat.getNodePath(treeNode);
                var d = JoyDialog({
                    titleText: '提示',
                    content:'是否要删除"' + path + '"？',
                    buttonTextCancel:"取消",
                    buttonTextConfirm: "删除",
                    onClickConfirmBtn: ClickConfirmBtn
                });
                function ClickConfirmBtn() {
                        JoyDialog.showLoading();
                        $.post( page_content_path+'/api/system/authorize/resource/delete',
                            {id: treeNode.pId,appid:treeNode.pAppid},
                            function (txt) {
                                JoyDialog.hideLoading();
                                if (!joyconn_layout.ValidataResult(txt)) {
                                    return;
                                }
                                if (txt.data > 0) {
                                    JoyDialog({
                                        type:"toast_success",
                                        infoText: '删除"' + path + '"成功！',
                                    });
                                   

                                    resource_operat.zTreeObj.removeNode(treeNode);
                                    for (var i in resource_operat.Right_permissions) {
                                        if (resource_operat.Right_permissions[i] == treeNode.pPermission) {
                                            resource_operat.Right_permissions.splice(i, 1);
                                            break;
                                        }
                                    }

                                }
                                else {
                                    JoyDialog({
                                        type:"toast_error",
                                        infoText:'删除"' + path + '失败！',
                                    });
                                    
                                }
                            }
                        );
                    }
               

                return false;
            });
        }
        //为树的节点移除鼠标悬浮效果
        function removeHoverDom (treeId, treeNode) {
            $("#diySpanStr_" + treeNode.tId).unbind().remove();
        }

        
        resource_operat.initPaths=function (path) {
            $("#rightUrl").empty();
            if (path) {
                $("#rightUrl").append('<option value="' + path + '" selected>' + path + '</option>');
                $("#rightUrl").append('<option value="#" >分类</option>');
            } else {
                $("#rightUrl").append('<option value="#" selected>分类</option>');

            }
            $.ajax({
                url:  page_content_path+'/api/system/authorize/resource/getAllPermission?appid='+resource_operat.appid,
                type: 'get',
                cache: true,
                success: function (result) {
                    if (!joyconn_layout.ValidataResult(result)) {
                        return;
                    }
                    if (result.data){
                        var urlPermissionMap={};
                        $(result.data).each(function (i,info) {                                
                            if ($.inArray(info.permission, resource_operat.Right_permissions) < 0 ) {
                                $("#rightUrl").append('<option value="' +info.permission + '">' + info.permission +'</option>');
                            }
                        })
                        $("#rightUrl").selectpicker('refresh')
                        if (path) {
                            $("#rightUrl").selectpicker('val',path);
                        }else{
                            $("#rightUrl").selectpicker('val',"");
                        }
                        function sideOut(url) {
                            var urls=","
                            +",";
                            if(urls.toLowerCase().indexOf(","+url.toLowerCase()+",")>-1){
                                return true;
                            }
                            return false;
                        }
                    }
                }
            });
        }



        resource_operat.getNodePath = function (treeNode) {
            var tempnode = treeNode;
            var path = '/' + treeNode.pName;
            while (tempnode.getParentNode() != null) {
                tempnode = tempnode.getParentNode();
                path = '/' + tempnode.pName + path;
            }
            path = path + "/";
            return path;
        }
        resource_operat.pageInit();
    </script>
</body>

</html>
{{end}}